package cn.wolfcode.wolf2w.search.controller;

import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.destination.api.domain.Destination;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import cn.wolfcode.wolf2w.note.api.domain.Note;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.IDestinationEsService;
import cn.wolfcode.wolf2w.search.service.INoteEsService;
import cn.wolfcode.wolf2w.search.service.IStrategyEsService;
import cn.wolfcode.wolf2w.search.service.IUserInfoEsService;
import cn.wolfcode.wolf2w.search.vo.SearchResultVO;
import cn.wolfcode.wolf2w.strategy.api.domain.Strategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SearchController {

    @Autowired
    private IStrategyEsService strategyEsService;
    @Autowired
    private INoteEsService noteEsService;
    @Autowired
    private IUserInfoEsService userInfoEsService;
    @Autowired
    private IDestinationEsService destinationEsService;

    @GetMapping("/search")
    public R<?> search(SearchQueryObject qo) {

        switch (qo.getType()) {
            case SearchQueryObject.TYPE_STRATEGY:
                return this.searchStrategy(qo);
            case SearchQueryObject.TYPE_NOTE:
                return this.searchNote(qo);
            case SearchQueryObject.TYPE_USER:
                return this.searchUser(qo);
            case SearchQueryObject.TYPE_ANSWER:
                return this.searchAnswer(qo);
            case SearchQueryObject.TYPE_DESTINATION:
                return this.searchDestination(qo);
            default:
                return this.searchAll(qo);
        }
    }

    private R<?> searchAll(SearchQueryObject qo) {
        SearchResultVO vo = new SearchResultVO();

        Page<Destination> dpage = destinationEsService.queryByKeyWord(qo);
        Page<UserInfo> upage = userInfoEsService.queryByKeyWord(qo);
        Page<Note> npage = noteEsService.queryByKeyWord(qo);
        Page<Strategy> spage = strategyEsService.queryByKeyWord(qo);

        vo.setDests(dpage.getContent());
        vo.setUsers(upage.getContent());
        vo.setStrategies(spage.getContent());
        vo.setNotes(npage.getContent());

        vo.setTotal(dpage.getTotalElements() + npage.getTotalElements() + upage.getTotalElements() + spage.getTotalElements());

        return R.ok(vo);
    }

    private R<?> searchAnswer(SearchQueryObject qo) {
        return null;
    }

    private R<?> searchDestination(SearchQueryObject qo) {
        Page<Destination> page = destinationEsService.queryByKeyWord(qo);
        return R.ok(page);
    }


    private R<?> searchUser(SearchQueryObject qo) {
        Page<UserInfo> page = userInfoEsService.queryByKeyWord(qo);
        return R.ok(page);
    }

    private R<?> searchNote(SearchQueryObject qo) {
        Page<Note> page = noteEsService.queryByKeyWord(qo);
        return R.ok(page);
    }

    private R<?> searchStrategy(SearchQueryObject qo) {
        Page<Strategy> page = strategyEsService.queryByKeyWord(qo);
        return R.ok(page);
    }

    @GetMapping("/dataInit")
    public R<?> dataInit() {
//        strategyEsService.initStrategyEs();
//        noteEsService.initNoteEs();
//        userInfoEsService.initUserinfoEs();
        destinationEsService.initDestinationEs();
        return R.ok();
    }
}
